const BASEURL = 'https://api.xzs51.com/v1/'
// http://static.xzs51.com https://api.xzs51.com/v1/
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
const formatRichText = html => {
  let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
    match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
    match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
    match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
    return match;
  });
  newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
    match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
    return match;
  });
  newContent = newContent.replace(/<br[^>]*\/>/gi, '');
  newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin-top:10px;margin-bottom:10px;"');
  return newContent;
}

const timeTranfer = (unixtimestamp,judge) => {

  var unixtimestamp = new Date(unixtimestamp * 1000);
  var year = 1900 + unixtimestamp.getYear();
  var month = "0" + (unixtimestamp.getMonth() + 1);
  var date = "0" + unixtimestamp.getDate();
  var hour = "0" + unixtimestamp.getHours();
  var minute = "0" + unixtimestamp.getMinutes();
  var second = "0" + unixtimestamp.getSeconds();
  if (!judge){
    return year + "/" + month.substring(month.length - 2, month.length) + "/" + date.substring(date.length - 2, date.length) +
      " " + hour.substring(hour.length - 2, hour.length) + ":" +
      minute.substring(minute.length - 2, minute.length) + ":" +
      second.substring(second.length - 2, second.length);
  }else if(judge==1){
    return year + "/" + month.substring(month.length - 2, month.length) + "/" + date.substring(date.length - 2, date.length)
  }else if(judge==2){
    return month.substring(month.length - 2, month.length) + "/" + date.substring(date.length - 2, date.length) +
      " " + hour.substring(hour.length - 2, hour.length) + ":" +
      minute.substring(minute.length - 2, minute.length)
  } else if (judge == 3) {
    return year + "-" + month.substring(month.length - 2, month.length) + "-" + date.substring(date.length - 2, date.length)
  }else{
    return  hour.substring(hour.length - 2, hour.length) + ":" +
      minute.substring(minute.length - 2, minute.length) + ":" +
      second.substring(second.length - 2, second.length);
  }
}
const productKind = {
  1: '音乐',
  2: '舞蹈',
  3: '科技',
  4: "书法",
  5: '绘画',
  6: '乐高',
  7: "演讲",
  8: '研学',
  9: "夏令营",
  10: '自然'
}

// ajax请求
const AJAX = (url, method, data, noneedLoading, config) => {
  if (!url) {
    return
  }
  if (!noneedLoading) {
    wx.showLoading({
      title: '加载中...',
    })
  }
  
  let _token = wx.getStorageSync("ACCESSTOKEN") || ''


  return new Promise((resolve, reject) =>
    wx.request({
      url: BASEURL + url,
      method: method || 'GET',
      data: data,
      header: {
        'content-type': 'application/x-www-form-urlencoded', // 默认值
        "access-token": _token,
        "source": "xiaochengxu",
        "version": "v1"
      },
      success(e) {
        if (e.data.status == "succeed") {
          let _data = e.data
          _data.header = e.header
          resolve(_data)
        } else {
          wx.showToast({
            title: e.data.message,
            icon: 'none'
          })
          reject(e.data)
        }
      },
      fail(e) {
        wx.showLoading({
          title: '网络错误'
        })
      },
      complete: res => {
        wx.hideLoading()
      }
    }))

}

module.exports = {
  formatTime: formatTime,
  AJAX: AJAX,
  timeTranfer: timeTranfer,
  productKind: productKind,
  formatRichText: formatRichText,

}